/**
 * 订单明细数据模型
 * @type {OrderList}
 */
/*********************************/
module.exports = class OrderList extends require('./model') {

  /**
   * 创建订单明细
   * @param order_list
   * @returns {Promise<unknown>}
   */
  static setOrderList(order_list) {
    return new Promise((resolve, reject) => {
      let sql = 'INSERT INTO order_list SET ?'

      console.log('插入订单明细' + sql);
      console.log('order_code = ' + order_list.order_code + '; goods_id = ' + order_list.goods_id +
        '; goodsprice = ' + order_list.goodsprice + '; goodscount = ' + order_list.goodscount + '; price = ' + order_list.price);

      this.dbQuery(sql, order_list)
        .then(result => {
          resolve(result.insertId)
        })
        .catch(error => {
          reject(error)
        })

    })
  }

  /**
   * 查询订单明细
   * @param order_code 订单编号
   * @returns {Promise<unknown>}
   */
  static getOrderList(order_code) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT order_list.*, goods.\`name\`
        FROM order_list, goods
        WHERE order_list.goods_id = goods.id AND 1 = 1 `

      sql += order_code && order_code != '' ? ` AND order_list.order_code = '${order_code}' ` : ` `

      console.log('查询订单明细sql=' + sql);
      console.log('order_code=' + order_code + ';-------------------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result)
        })
        .catch(error => {
          reject(error)
        })
    })
  }

  /**
   * 获取交易明细条数，
   * @param id 交易明细id
   * @param order_code 订单编码
   * @param buy_username 买家用户名
   * @param bus_username 商家用户名
   * @param goodsCategoryId 商品类目id
   * @param rootCategoryId 主类目id
   * @param lessPrice 大于的价格
   * @param morePrice 小于的价格
   * @param lessDate 大于的日期
   * @param moreDate 小于的日期
   * @returns {Promise<unknown>}
   */
  static getTransactionCount(id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT COUNT(1) AS count FROM \`order\`, order_list, goods, goods_category, root_category 
        WHERE 1 = 1 AND \`order\`.\`code\` = order_list.order_code AND order_list.goods_id = goods.id 
        AND goods_category.id = goods.goods_category_id AND root_category.id = goods_category.root_category_id `
      sql += id && id != 0 ? ` AND order_list.id = ${id} ` : ` `
      sql += order_code && order_code != '' ? ` AND order_list.order_code = '${order_code}' ` : ` `
      sql += buy_username && buy_username != '' ? ` AND \`order\`.user_username = '${buy_username}' ` : ` `
      sql += bus_username && bus_username != '' ? ` AND goods_category.user_username = '${bus_username}' ` : ` `
      sql += goodsCategoryId && goodsCategoryId != 0 ? ` AND goods_category.id = ${goodsCategoryId} ` : ` `
      sql += rootCategoryId &&rootCategoryId != 0 ? ` AND root_category.id = ${rootCategoryId} ` : ` `
      sql += lessPrice ? ` AND order_list.price >= ${lessPrice} ` : ` `
      sql += morePrice && morePrice != 0 ? ` AND order_list.price <= ${morePrice} ` : ` `
      sql += lessDate ? ` AND \`order\`.time >= '${lessDate}' ` : ` `
      sql += moreDate ? ` AND \`order\`.time <= '${moreDate}' ` : ` `

      // sql += ` ORDER BY order_list.price DESC LIMIT ?, ?  `

      console.log('查询交易条数sql=' + sql);
      console.log('id=' + id + '; order_code=' + order_code + '; buy=' + buy_username + '; bus=' + bus_username +
        '; goodsCategoryId=' + goodsCategoryId + '; rootCategoryId=' + rootCategoryId + '; lessPrice=' + lessPrice +
        '; morePrice=' + morePrice + '; lessDate=' + lessDate + '; moreDate=' + moreDate + ';------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result[0])
        })
        .catch(error => {
          reject(error)
        })
    })
  }


  /**
   * 获取交易明细,如购买记录、卖出记录
   * @param start 起始记录
   * @param size 记录条数
   * @param id 交易明细id
   * @param order_code 订单编码
   * @param buy_username 买家用户名
   * @param bus_username 商家用户名
   * @param goodsCategoryId 商品类目id
   * @param rootCategoryId 主类目id
   * @param lessPrice 大于的价格
   * @param morePrice 小于的价格
   * @param lessDate 大于的日期
   * @param moreDate 小于的日期
   * @returns {Promise<unknown>}
   */
  static getTransactionList(start, size, id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT order_list.id order_list_id, \`order\`.user_username buy_username, \`order\`.time, 
        order_list.order_code, order_list.goodsprice sell_price, order_list.goodscount sell_count, 
        order_list.price earn_price, goods.id, goods.\`name\`, goods.src, goods_category.id goods_category_id, 
        goods_category.\`name\` goods_category_name, goods_category.user_username bus_username, 
        root_category.id root_category_id, root_category.\`name\` root_category_name 
        FROM \`order\`, order_list, goods, goods_category, root_category 
        WHERE 1 = 1 AND \`order\`.\`code\` = order_list.order_code AND order_list.goods_id = goods.id 
        AND goods_category.id = goods.goods_category_id AND root_category.id = goods_category.root_category_id `
      sql += id && id != 0 ? ` AND order_list.id = ${id} ` : ` `
      sql += order_code && order_code != '' ? ` AND order_list.order_code = '${order_code}' ` : ` `
      sql += buy_username && buy_username != '' ? ` AND \`order\`.user_username = '${buy_username}' ` : ` `
      sql += bus_username && bus_username != '' ? ` AND goods_category.user_username = '${bus_username}' ` : ` `
      sql += goodsCategoryId && goodsCategoryId != 0 ? ` AND goods_category.id = ${goodsCategoryId} ` : ` `
      sql += rootCategoryId &&rootCategoryId != 0 ? ` AND root_category.id = ${rootCategoryId} ` : ` `
      sql += lessPrice ? ` AND order_list.price >= ${lessPrice} ` : ` `
      sql += morePrice && morePrice != 0 ? ` AND order_list.price <= ${morePrice} ` : ` `
      sql += lessDate ? ` AND \`order\`.time >= '${lessDate}' ` : ` `
      sql += moreDate ? ` AND \`order\`.time <= '${moreDate}' ` : ` `

      sql += ` ORDER BY order_list.price DESC LIMIT ?, ?  `

      console.log('查询交易条数sql=' + sql);
      console.log('start=' + start + '; size=' + size + ';id=' + id + '; order_code=' + order_code + '; buy=' + buy_username + '; bus=' + bus_username +
        '; goodsCategoryId=' + goodsCategoryId + '; rootCategoryId=' + rootCategoryId + '; lessPrice=' + lessPrice +
        '; morePrice=' + morePrice + '; lessDate=' + lessDate + '; moreDate=' + moreDate + ';------------------------');

      this.dbQuery(sql, [start, size])
        .then(result => {
          resolve(result)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}